package com.devops.controller;

import cn.hutool.core.util.ObjectUtil;
import com.devops.entity.req.DockerReq;
import com.devops.entity.resp.ErrorReason;
import com.devops.entity.resp.ResponseResult;
import com.devops.service.DockerService;
import com.devops.utils.ResponseUtils;
import com.devops.utils.error.ReturnErrorException;
import com.devops.utils.error.ReturnResultType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/docker")
@Tag(name  = "docker连接")
public class DockerController {

    @Autowired
    private DockerService dockerService;

    @PostMapping("/find")
    @Operation(summary = "docker版本查询")
    public ResponseResult getVersion(@RequestBody DockerReq dockerReq) throws Exception {
        String versionInfo = dockerService.getVersion(dockerReq);
        if (ObjectUtil.isEmpty(versionInfo)) {
            throw new ReturnErrorException(ReturnResultType.OPERATOR_ERROR, new ErrorReason("未安装docker"));
        } else {
            return ResponseUtils.success(dockerService.getVersion(dockerReq));
        }
    }

    @PostMapping("/install")
    @Operation(summary = "docker安装（会先卸载docker）")
    public ResponseResult install(@RequestBody DockerReq dockerReq) throws Exception {
        dockerService.install(dockerReq);
        return ResponseUtils.success("可能安装成功了？");
    }

    @PostMapping("/delete")
    @Operation(summary = "docker卸载")
    public ResponseResult delete(@RequestBody DockerReq dockerReq) throws Exception {
        dockerService.delete(dockerReq);
        return ResponseUtils.success("可能删除成功了？");
    }

}
